// 这份代码专门处理逻辑层的操作
const circleDao = require('../dao/circleDao');
const powerDao = require('../dao/powerDao');

const circleService = {
    /*
     * 懒加载获取圈子接口
     */
    async getCircle(uid, lastId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看圈子');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.getCircle(uid, lastId);
            result = { success: true, msg: '获取圈子成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取圈子详细信息
     */
    async getCircleInfo(uid, circleId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看圈子');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.getCircleInfo(uid, circleId);
            if (data.length) {
                result = { success: true, msg: '获取圈子成功', data: data[0] };
            } else {
                result = { success: false, msg: '该圈子不存在' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 评论圈子
     */
    async comment(uid, circleId, comment) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看圈子');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.comment(uid, circleId, comment);
            if (data.insertId) {
                result = { success: true, msg: '评论圈子成功' };
            } else {
                result = { success: false, msg: '评论圈子失败' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取所有评论信息
     */
    async getComment(circleId, uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看圈子');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.getComment(circleId);
            result = { success: true, msg: '获取评论成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 点赞圈子
     */
    async likeCircle(uid, circleId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看圈子');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.likeCircle(uid, circleId);
            if (data.insertId) {
                result = { success: true, msg: '点赞圈子成功' };
            } else {
                result = { success: false, msg: '不能重复点赞' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    //
    async nolikeCircle(uid, circleId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看圈子');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.nolikeCircle(uid, circleId);
            if (data.affectedRows) {
                result = { success: true, msg: '取消点赞圈子成功' };
            } else {
                result = { success: false, msg: '取消点赞圈子失败' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async getAllCircle(uid, page) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '帖子管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.getAllCircle(uid, page);
            result = { success: true, msg: '获取所有圈子成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },

    // 发布圈子
    async addCircle(uid, circleTitle, circleContent, selectedImage) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看圈子');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.addCircle(uid, circleTitle, circleContent, selectedImage);
            result = { success: true, msg: '发布圈子成功', data: data.insertId };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    // 删除圈子
    async deleteCircle(uid, circleId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看圈子');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.deleteCircle(uid, circleId);
            if (data.affectedRows == 0) {
                result = { success: false, msg: '删除圈子失败' };
            } else {
                result = { success: true, msg: '删除圈子成功' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },

    async adminDeleteCircle(uid, circleId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '帖子管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await circleDao.adminDeleteCircle(circleId);
            if (data.affectedRows == 0) {
                result = { success: false, msg: '删除圈子失败' };
            } else {
                result = { success: true, msg: '删除圈子成功' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
};

module.exports = circleService;